package com.lu.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lu.entity.Phone;
import com.lu.entity.User;
import com.lu.phone.utils.MobileCodeWS;
import com.lu.phone.utils.MobileCodeWSSoap;
import com.lu.service.PhoneService;
import com.lu.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class DoUpdateAction extends ActionSupport{
	
	UserService userService = new UserService();
	PhoneService phoneService = new PhoneService();
	
	private Long id;
	private String password;
	private String phoneNO;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	public String execute() throws Exception{
		
		if(id==null){
			return "error";
		}
		User user = new User();
		user.setId(id);
		user.setPassword(password);
		user.setPhone(phoneNO);
		int rs = userService.update(user);
		
		if((rs>0)&&(phoneNO!=null||!phoneNO.equals(""))){
			if(id!=null){
				
				Map<String,Object> map = new HashMap<>();
				map.put("user_id",id.toString());
				Phone p = phoneService.getPhone(map);
				if(p.getId()!=null){
					MobileCodeWS mobileCodeWS = new MobileCodeWS();
					MobileCodeWSSoap mobileCodeWSSoap= mobileCodeWS.getMobileCodeWSSoap();
					String str = mobileCodeWSSoap.getMobileCodeInfo(phoneNO, null);
					int checkPhoneNO = str.indexOf("：");
					str = str.substring(str.indexOf("：")+1, str.length());
					String[] ss = str.split(" ");
					
					if(checkPhoneNO!=-1){
						Phone phone = new Phone();
						for(int i=0;i<ss.length;i++){
							phone.setProvince(ss[0]);
							phone.setCity(ss[1]);
							phone.setCardType(ss[2]);
						}
						phone.setId(p.getId());
						phoneService.update(phone);
					}
				}else{
					MobileCodeWS mobileCodeWS = new MobileCodeWS();
					MobileCodeWSSoap mobileCodeWSSoap= mobileCodeWS.getMobileCodeWSSoap();
					String str = mobileCodeWSSoap.getMobileCodeInfo(phoneNO, null);
					int checkPhoneNO = str.indexOf("：");
					str = str.substring(str.indexOf("：")+1, str.length());
					String[] ss = str.split(" ");
					
					if(checkPhoneNO!=-1){
						Phone phone = new Phone();
						for(int i=0;i<ss.length;i++){
							phone.setProvince(ss[0]);
							phone.setCity(ss[1]);
							phone.setCardType(ss[2]);
						}
						phone.setUserId(id);
						phoneService.save(phone);
				}
			}
		}
		}
		return "success";
	}


	public Long getId() {
		return id;
	}


	public void setId(Long id) {
		this.id = id;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public String getPhoneNO() {
		return phoneNO;
	}


	public void setPhoneNO(String phoneNO) {
		this.phoneNO = phoneNO;
	}

}
